﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anuntiomatic.Server
{
    public partial class Login : BasePage
    {
        string encryptKey = "6QgOFHJX8uqCCYXRmOrD5yrNlb7ij8sgJ/wpjKWHd/s=";//Key mã hóa
        string iv = "MSfaUAn0CVfLx6W2ZGS5BQ==";//Vector mã hóa
        
        protected override void OnInit(EventArgs e)
        {
            //base.OnInit(e);
            if (UserID != null)
                RedirectToPage(DefaultURL);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtPassword.Focus();
                HttpCookie cookies = Request.Cookies[COOKIES.LOGIN];
                if (cookies != null && cookies.Values[COOKIES.USERNAME] != string.Empty)
                {
                    txtUserName.Text = Request.Cookies[COOKIES.LOGIN][COOKIES.USERNAME];
                    try
                    {
                        txtPassword.Attributes["value"] = Request.Cookies[COOKIES.LOGIN][COOKIES.PASSWORD].AESDecryptString(encryptKey, iv);
                    }
                    catch { }
                    RememberMe.Checked = true;
                    LoginButton.Focus();
                }
                else
                {
                    txtUserName.Focus();
                }
            }
        }

        protected override bool RequiredLogin()
        {
            return false;
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                SysUser user = SysUser.Login(txtUserName.Text, txtPassword.Text);
                if (user != null)
                {
                    if (user.Disabled || user.Deleted == true)
                    {
                        lbMessage.Text = "Sai tài khoản hoặc mật khẩu!";
                        return;
                    }
                    HttpCookie cookies;
                    if (RememberMe.Checked)
                    {
                        cookies = Request.Cookies[COOKIES.LOGIN];
                        if (cookies == null)
                        {
                            cookies = new HttpCookie(COOKIES.LOGIN);
                        }
                        cookies.Values[COOKIES.USERNAME] = txtUserName.Text.Trim();
                        try
                        {
                            cookies.Values[COOKIES.PASSWORD] = txtPassword.Text.ToAESEncryptString(encryptKey, iv);
                        }
                        catch { }
                        cookies.Expires = DateTime.Now.AddMonths(1);
                        Response.Cookies.Add(cookies);
                    }
                    else
                    {
                        cookies = Request.Cookies[COOKIES.LOGIN];
                        if (cookies != null)
                        {
                            cookies.Values[COOKIES.USERNAME] = string.Empty;
                            Response.Cookies.Add(cookies);
                        }
                    }
                    UserID = user.UserID;
                    if (user.RequiredChangePassword == null || user.RequiredChangePassword == true)
                    {
                        RedirectToPage(ChangePasswordURL);
                        return;
                    }
                    string defaultUrl = SysMenu.GetDefaultMenu(user.UserID);
                    if (defaultUrl == "")
                        defaultUrl = ReturnURL;
                    if (!string.IsNullOrEmpty(defaultUrl))
                    {
                        RedirectToPage(defaultUrl);
                        return;
                    }
                    else
                    {
                        RedirectToPage(DefaultURL);
                        return;
                    }
                }
                else
                {
                    lbMessage.Text = "Sai tài khoản hoặc mật khẩu!";
                }
            }
            catch { }
        }
    }
}
